###############################################################
# Values, Taboos, and Votes: 
# How Basic Human Values Affect Populist Electoral Support
#
# Ugur Ozdemir, Marc S. Jacob
# 2021
# MAIN
###############################################################

# Packages and data -------------------------------------------------------

pckgs <-
  c("dplyr",
    "data.table",
    "foreign",
    "lavaan",
    "memisc",
    "car",
    "ggplot2",
    "gridExtra",
    "pracma")

lapply(pckgs, require, character.only = TRUE)

gr17 <- read.spss("ZA6804_v6-0-0.sav",to.data.frame = TRUE,use.value.labels = FALSE) # loading data
gr17 <- filter(gr17, w1b == "1") # select observations from wave 1

sem.data <-
  dplyr::select(
    gr17,
    kp1_170,
    kp1_2100,
    kp1_2090a,
    kp1_190ba,
    kp1_1090,
    kp1_1130,
    kp1_1210,
    kp1_1250,
    kp1_1290,
    kp1_1411,
    kp1_2591,
    kp1_3910,
    kp1_2320,
    kpx_2290,
    kpx_2280,
    kp1_1500,
    contains("3320"),
    kp1_3680,
    kp1_2580,
    kp9_200ba
  ) # select variables

# Preparing variables -----------------------------------------------------

oldnames <-
  c(
    "kp1_1090",
    "kp1_3910",
    "kp1_2320",
    "kp1_2591",
    "kpx_2280",
    "kp1_2580",
    "kp1_1411",
    "kp1_1130",
    "kp1_1290",
    "kp1_1250",
    "kp1_1210"
  ) 
newnames <-
  c(
    "SocEcon1",
    "Relig",
    "Educ",
    "Income",
    "Gender",
    "Class",
    "SecPriv1",
    "Immigrat1",
    "Climate1",
    "EuInteg1",
    "MigInteg1"
  )

setnames(sem.data, oldnames, newnames)

sem.data$Age <- 2016 - sem.data$kpx_2290 # compute age variable

sem.data$Relig <- recode(sem.data$Relig, "1=5;2=4;3=3;4=2;5=1") # recoding religion (5 (new) = very religious)

sem.data$Gender <- recode(sem.data$Gender, "2=1;1=0") #recoding gender (1 (new) = female)

# set party preference

sem.data$AfD <- ordered(ifelse(sem.data$kp1_190ba == 322, 1, 0))
sem.data$Left <- ordered(ifelse(sem.data$kp1_190ba == 7, 1, 0))
sem.data$SPD <- ordered(ifelse(sem.data$kp1_190ba == 4, 1, 0))
sem.data$Greens <- ordered(ifelse(sem.data$kp1_190ba == 6, 1, 0))
sem.data$CDU <- ordered(ifelse(sem.data$kp1_190ba == 1, 1, 0))
sem.data$FDP <- ordered(ifelse(sem.data$kp1_190ba == 5, 1, 0))

# set issue opinions ordered (for ordinal regression of values on issue opinions)

sem.data$Immigrat1 <- ordered(sem.data$Immigrat1)
sem.data$SecPriv1 <- ordered(sem.data$SecPriv1)
sem.data$EuInteg1 <- ordered(sem.data$EuInteg1)
sem.data$SocEcon1 <- ordered(sem.data$SocEcon1)
sem.data$MigInteg1 <- ordered(sem.data$MigInteg1)
sem.data$Climate1 <- ordered(sem.data$Climate1)

# Tables 1-4: SEM models --------------------------------------------------

# CDU

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
CDU ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1
+ b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec
+ Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'
fitCDU <- sem(model, data = sem.data)


# SPD

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
SPD ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitSPD <- sem(model, data = sem.data)

# AfD

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
AfD ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitAfD <- sem(model, data = sem.data)

# Greens

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
Greens ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income+ Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income+ Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income+ Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income+ Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income+ Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income+ Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitGreens <- sem(model, data = sem.data)

# Left

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
Left ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income+ Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income+ Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income+ Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income+ Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income+ Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income+ Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitLeft <- sem(model, data = sem.data)

# FDP

model <- '
# 1. measurement model
sec =~ kp1_3320e + kp1_3320m
univ =~ kp1_3320c + kp1_3320h + kp1_3320u
conf =~ kp1_3320n + kp1_3320q

# outcome model
FDP ~ c1*sec + c2*univ + c3*conf + b1*Immigrat1 + b2*MigInteg1 + b3*SecPriv1 + b4*EuInteg1 + b5*SocEcon1 + b6*Climate1 + Age + Income+ Gender + Educ + Relig

# mediator models
Immigrat1 ~ a11*sec + a12*univ + a13*conf + Age + Income + Gender + Educ + Relig
MigInteg1 ~ a21*sec + a22*univ + a23*conf + Age + Income + Gender + Educ + Relig
SecPriv1 ~ a31*sec + a32*univ + a33*conf + Age + Income + Gender + Educ + Relig
EuInteg1 ~ a41*sec + a42*univ + a43*conf + Age + Income + Gender + Educ + Relig
SocEcon1 ~ a51*sec + a52*univ + a53*conf + Age + Income + Gender + Educ + Relig
Climate1 ~ a61*sec + a62*univ + a63*conf + Age + Income + Gender + Educ + Relig

# indirect effects (IDE)
Immigrat1IDESec := b1*a11
MigInteg1IDESec := b2*a21
SecPriv1IDESec  := b3*a31
EuInteg1IDESec  := b4*a41
SocEcon1IDESec  := b5*a51
Climate1IDESec  := b6*a61
sumIDESec := Immigrat1IDESec + MigInteg1IDESec + SecPriv1IDESec + EuInteg1IDESec + SocEcon1IDESec + Climate1IDESec

Immigrat1IDEUniv := b1*a12
MigInteg1IDEUniv := b2*a22
SecPriv1IDEUniv  := b3*a32
EuInteg1IDEUniv  := b4*a42
SocEcon1IDEUniv  := b5*a52
Climate1IDEUniv   := b6*a62
sumIDEUniv := Immigrat1IDEUniv + MigInteg1IDEUniv + SecPriv1IDEUniv + EuInteg1IDEUniv + SocEcon1IDEUniv + Climate1IDEUniv

Immigrat1IDEConf := b1*a13
MigInteg1IDEConf := b2*a23
SecPriv1IDEConf := b3*a33
EuInteg1IDEConf  := b4*a43
SocEcon1IDEConf  := b5*a53
Climate1IDEConf := b6*a63
sumIDEConf := Immigrat1IDEConf + MigInteg1IDEConf + SecPriv1IDEConf + EuInteg1IDEConf + SocEcon1IDEConf + Climate1IDEConf

# total effects
totalSec  := c1 + sumIDESec
totalUniv := c2 + sumIDEUniv
totalConf := c3 + sumIDEConf

# model correlation between mediators
Immigrat1 ~~ EuInteg1
Immigrat1 ~~ MigInteg1
EuInteg1 ~~ MigInteg1
Climate1 ~~ MigInteg1
sec ~~ univ
conf ~~ sec
conf ~~ univ
'

fitFDP <- sem(model, data = sem.data)

fitMeasures(fitAfD, c("chisq", "df", "cfi", "tli", "rmsea")) #AfD
summary(fitAfD, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitLeft, c("chisq", "df", "cfi", "tli", "rmsea")) #Left
summary(fitLeft, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitCDU, c("chisq", "df", "cfi", "tli", "rmsea")) #CDU
summary(fitCDU, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitSPD, c("chisq", "df", "cfi", "tli", "rmsea")) #SPD
summary(fitSPD, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitGreens, c("chisq", "df", "cfi", "tli", "rmsea")) #Greens
summary(fitGreens, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

fitMeasures(fitFDP, c("chisq", "df", "cfi", "tli", "rmsea")) #FDP
summary(fitFDP, standardized = TRUE, fit.measures = TRUE, std.nox=TRUE)

# Prepare marginal effects plot -------------------------------------------

# CDU

### Initialization ###
fitModelCDUcon <- fitCDU

FactPredictCDUcon <- lavPredict(fitModelCDUcon)

parasCDUcon <- parameterEstimates(fitCDU)

sec <-mean(FactPredictCDUcon[, 1])
univ <- mean(FactPredictCDUcon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridCDUcon <-
  seq(
    from = min(FactPredictCDUcon[, 3]),
    to = max(FactPredictCDUcon[, 3]),
    len = 5000
  )

pred.probCDUcon <- list()
pred.probCDUcon.l <- list()
pred.probCDUcon.u <- list()


for (i in 1:length(plot.gridCDUcon)) {
  conf <- plot.gridCDUcon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasCDUcon[22:29,5];
  coefs.l <- parasCDUcon[22:29,9];
  coefs.u <- parasCDUcon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasCDUcon[30:37,5];
  coefs.l <- parasCDUcon[30:37,9];
  coefs.u <- parasCDUcon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasCDUcon[38:45,5];
  coefs.l <- parasCDUcon[38:45,9];
  coefs.u <- parasCDUcon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasCDUcon[46:53,5];
  coefs.l <- parasCDUcon[46:53,9];
  coefs.u <- parasCDUcon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasCDUcon[54:61,5];
  coefs.l <- parasCDUcon[54:61,9];
  coefs.u <- parasCDUcon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasCDUcon[62:69,5];
  coefs.l <- parasCDUcon[62:69,9];
  coefs.u <- parasCDUcon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasCDUcon[8:21,5];
  coefs.l <- parasCDUcon[8:21,9];
  coefs.u <- parasCDUcon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probCDUcon[i] <- pnorm(probits);
  pred.probCDUcon.l[i] <- pnorm(probits.l);
  pred.probCDUcon.u[i] <- pnorm(probits.u);
  
  
}

plot.dfCDUcon <-
  as.data.frame(
    cbind(plot.gridCDUcon,pred.probCDUcon,pred.probCDUcon.l,pred.probCDUcon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

CDUconf <-
  ggplot(plot.dfCDUcon, aes(x = as.numeric(plot.dfCDUcon[, 1]), y = as.numeric(plot.dfCDUcon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfCDUcon[, 3]), ymax=as.numeric(plot.dfCDUcon[, 4])), alpha = 0.15) +
  labs(subtitle = "CDU",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

CDUconf

# SPD

### Initialization ###
fitModelSPDcon <- fitSPD

FactPredictSPDcon <- lavPredict(fitModelSPDcon)

parasSPDcon <- parameterEstimates(fitSPD)

sec <-mean(FactPredictSPDcon[, 1])
univ <- mean(FactPredictSPDcon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridSPDcon <-
  seq(
    from = min(FactPredictSPDcon[, 3]),
    to = max(FactPredictSPDcon[, 3]),
    len = 5000
  )

pred.probSPDcon <- list()
pred.probSPDcon.l <- list()
pred.probSPDcon.u <- list()


for (i in 1:length(plot.gridSPDcon)) {
  conf <- plot.gridSPDcon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasSPDcon[22:29,5];
  coefs.l <- parasSPDcon[22:29,9];
  coefs.u <- parasSPDcon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasSPDcon[30:37,5];
  coefs.l <- parasSPDcon[30:37,9];
  coefs.u <- parasSPDcon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasSPDcon[38:45,5];
  coefs.l <- parasSPDcon[38:45,9];
  coefs.u <- parasSPDcon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasSPDcon[46:53,5];
  coefs.l <- parasSPDcon[46:53,9];
  coefs.u <- parasSPDcon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasSPDcon[54:61,5];
  coefs.l <- parasSPDcon[54:61,9];
  coefs.u <- parasSPDcon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasSPDcon[62:69,5];
  coefs.l <- parasSPDcon[62:69,9];
  coefs.u <- parasSPDcon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasSPDcon[8:21,5];
  coefs.l <- parasSPDcon[8:21,9];
  coefs.u <- parasSPDcon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probSPDcon[i] <- pnorm(probits);
  pred.probSPDcon.l[i] <- pnorm(probits.l);
  pred.probSPDcon.u[i] <- pnorm(probits.u);
  
  
}

plot.dfSPDcon <-
  as.data.frame(
    cbind(plot.gridSPDcon,pred.probSPDcon,pred.probSPDcon.l,pred.probSPDcon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

SPDconf <-
  ggplot(plot.dfSPDcon, aes(x = as.numeric(plot.dfSPDcon[, 1]), y = as.numeric(plot.dfSPDcon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfSPDcon[, 3]), ymax=as.numeric(plot.dfSPDcon[, 4])), alpha = 0.15) +
  labs(subtitle = "SPD",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

SPDconf

# Greens 

### Initialization ###
fitModelGreenscon <- fitGreens

FactPredictGreenscon <- lavPredict(fitModelGreenscon)

parasGreenscon <- parameterEstimates(fitGreens)

sec <-mean(FactPredictGreenscon[, 1])
univ <- mean(FactPredictGreenscon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridGreenscon <-
  seq(
    from = min(FactPredictGreenscon[, 3]),
    to = max(FactPredictGreenscon[, 3]),
    len = 5000
  )

pred.probGreenscon <- list()
pred.probGreenscon.l <- list()
pred.probGreenscon.u <- list()

for (i in 1:length(plot.gridGreenscon)) {
  conf <- plot.gridGreenscon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasGreenscon[22:29,5];
  coefs.l <- parasGreenscon[22:29,9];
  coefs.u <- parasGreenscon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasGreenscon[30:37,5];
  coefs.l <- parasGreenscon[30:37,9];
  coefs.u <- parasGreenscon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasGreenscon[38:45,5];
  coefs.l <- parasGreenscon[38:45,9];
  coefs.u <- parasGreenscon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasGreenscon[46:53,5];
  coefs.l <- parasGreenscon[46:53,9];
  coefs.u <- parasGreenscon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasGreenscon[54:61,5];
  coefs.l <- parasGreenscon[54:61,9];
  coefs.u <- parasGreenscon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasGreenscon[62:69,5];
  coefs.l <- parasGreenscon[62:69,9];
  coefs.u <- parasGreenscon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasGreenscon[8:21,5];
  coefs.l <- parasGreenscon[8:21,9];
  coefs.u <- parasGreenscon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probGreenscon[i] <- pnorm(probits);
  pred.probGreenscon.l[i] <- pnorm(probits.l);
  pred.probGreenscon.u[i] <- pnorm(probits.u);
  
  
}

plot.dfGreenscon <-
  as.data.frame(
    cbind(plot.gridGreenscon,pred.probGreenscon,pred.probGreenscon.l,pred.probGreenscon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

Greensconf <-
  ggplot(plot.dfGreenscon, aes(x = as.numeric(plot.dfGreenscon[, 1]), y = as.numeric(plot.dfGreenscon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfGreenscon[, 3]), ymax=as.numeric(plot.dfGreenscon[, 4])), alpha = 0.15) +
  labs(subtitle = "Greens",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

Greensconf

# FDP

### Initialization ###
fitModelFDPcon <- fitFDP

FactPredictFDPcon <- lavPredict(fitModelFDPcon)

parasFDPcon <- parameterEstimates(fitFDP)

sec <-mean(FactPredictFDPcon[, 1])
univ <- mean(FactPredictFDPcon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridFDPcon <-
  seq(
    from = min(FactPredictFDPcon[, 3]),
    to = max(FactPredictFDPcon[, 3]),
    len = 5000
  )

pred.probFDPcon <- list()
pred.probFDPcon.l <- list()
pred.probFDPcon.u <- list()


for (i in 1:length(plot.gridFDPcon)) {
  conf <- plot.gridFDPcon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasFDPcon[22:29,5];
  coefs.l <- parasFDPcon[22:29,9];
  coefs.u <- parasFDPcon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasFDPcon[30:37,5];
  coefs.l <- parasFDPcon[30:37,9];
  coefs.u <- parasFDPcon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasFDPcon[38:45,5];
  coefs.l <- parasFDPcon[38:45,9];
  coefs.u <- parasFDPcon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasFDPcon[46:53,5];
  coefs.l <- parasFDPcon[46:53,9];
  coefs.u <- parasFDPcon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasFDPcon[54:61,5];
  coefs.l <- parasFDPcon[54:61,9];
  coefs.u <- parasFDPcon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasFDPcon[62:69,5];
  coefs.l <- parasFDPcon[62:69,9];
  coefs.u <- parasFDPcon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasFDPcon[8:21,5];
  coefs.l <- parasFDPcon[8:21,9];
  coefs.u <- parasFDPcon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probFDPcon[i] <- pnorm(probits);
  pred.probFDPcon.l[i] <- pnorm(probits.l);
  pred.probFDPcon.u[i] <- pnorm(probits.u);
  
}

plot.dfFDPcon <-
  as.data.frame(
    cbind(plot.gridFDPcon,pred.probFDPcon,pred.probFDPcon.l,pred.probFDPcon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

FDPconf <-
  ggplot(plot.dfFDPcon, aes(x = as.numeric(plot.dfFDPcon[, 1]), y = as.numeric(plot.dfFDPcon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfFDPcon[, 3]), ymax=as.numeric(plot.dfFDPcon[, 4])), alpha = 0.15) +
  labs(subtitle = "FDP",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

FDPconf

# AfD 

# Conformity

### Initialization ###
fitModelAfDcon <- fitAfD

FactPredictAfDcon <- lavPredict(fitModelAfDcon)

parasAfDcon <- parameterEstimates(fitAfD)

sec <-mean(FactPredictAfDcon[, 1])
univ <- mean(FactPredictAfDcon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridAfDcon <-
  seq(
    from = min(FactPredictAfDcon[, 3]),
    to = max(FactPredictAfDcon[, 3]),
    len = 5000
  )

pred.probAfDcon <- list()
pred.probAfDcon.l <- list()
pred.probAfDcon.u <- list()


for (i in 1:length(plot.gridAfDcon)) {
  conf <- plot.gridAfDcon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasAfDcon[22:29,5];
  coefs.l <- parasAfDcon[22:29,9];
  coefs.u <- parasAfDcon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasAfDcon[30:37,5];
  coefs.l <- parasAfDcon[30:37,9];
  coefs.u <- parasAfDcon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasAfDcon[38:45,5];
  coefs.l <- parasAfDcon[38:45,9];
  coefs.u <- parasAfDcon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasAfDcon[46:53,5];
  coefs.l <- parasAfDcon[46:53,9];
  coefs.u <- parasAfDcon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasAfDcon[54:61,5];
  coefs.l <- parasAfDcon[54:61,9];
  coefs.u <- parasAfDcon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasAfDcon[62:69,5];
  coefs.l <- parasAfDcon[62:69,9];
  coefs.u <- parasAfDcon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasAfDcon[8:21,5];
  coefs.l <- parasAfDcon[8:21,9];
  coefs.u <- parasAfDcon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probAfDcon[i] <- pnorm(probits);
  pred.probAfDcon.l[i] <- pnorm(probits.l);
  pred.probAfDcon.u[i] <- pnorm(probits.u);
  
  
}

plot.dfAfDcon <-
  as.data.frame(
    cbind(plot.gridAfDcon,pred.probAfDcon,pred.probAfDcon.l,pred.probAfDcon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

AfDconf <-
  ggplot(plot.dfAfDcon, aes(x = as.numeric(plot.dfAfDcon[, 1]), y = as.numeric(plot.dfAfDcon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfAfDcon[, 3]), ymax=as.numeric(plot.dfAfDcon[, 4])), alpha = 0.15) +
  labs(subtitle = "AfD",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

AfDconf

# Universalism


### Initialization ###
fitModelAfDcon <- fitAfD

FactPredictAfDcon <- lavPredict(fitModelAfDcon)

parasAfDcon <- parameterEstimates(fitAfD)

sec <-mean(FactPredictAfDcon[, 1])
univ <- mean(FactPredictAfDcon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridAfDcon <-
  seq(
    from = min(FactPredictAfDcon[, 3]),
    to = max(FactPredictAfDcon[, 3]),
    len = 5000
  )

pred.probAfDcon <- list()
pred.probAfDcon.l <- list()
pred.probAfDcon.u <- list()


for (i in 1:length(plot.gridAfDcon)) {
  conf <- plot.gridAfDcon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasAfDcon[22:29,5];
  coefs.l <- parasAfDcon[22:29,9];
  coefs.u <- parasAfDcon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasAfDcon[30:37,5];
  coefs.l <- parasAfDcon[30:37,9];
  coefs.u <- parasAfDcon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasAfDcon[38:45,5];
  coefs.l <- parasAfDcon[38:45,9];
  coefs.u <- parasAfDcon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasAfDcon[46:53,5];
  coefs.l <- parasAfDcon[46:53,9];
  coefs.u <- parasAfDcon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasAfDcon[54:61,5];
  coefs.l <- parasAfDcon[54:61,9];
  coefs.u <- parasAfDcon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasAfDcon[62:69,5];
  coefs.l <- parasAfDcon[62:69,9];
  coefs.u <- parasAfDcon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasAfDcon[8:21,5];
  coefs.l <- parasAfDcon[8:21,9];
  coefs.u <- parasAfDcon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probAfDcon[i] <- pnorm(probits);
  pred.probAfDcon.l[i] <- pnorm(probits.l);
  pred.probAfDcon.u[i] <- pnorm(probits.u);
  
}

plot.dfAfDcon <-
  as.data.frame(
    cbind(plot.gridAfDcon,pred.probAfDcon,pred.probAfDcon.l,pred.probAfDcon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

AfDconf <-
  ggplot(plot.dfAfDcon, aes(x = as.numeric(plot.dfAfDcon[, 1]), y = as.numeric(plot.dfAfDcon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfAfDcon[, 3]), ymax=as.numeric(plot.dfAfDcon[, 4])), alpha = 0.15) +
  labs(subtitle = "AfD",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

AfDconf

# Security

### SECURITY ###

### AfD ### (For other parties just replace AfD with the name of the party.)

### Initialization ###
fitModelAfDsec <- fitAfD

FactPredictAfDsec <- FactPredictAfDcon

parasAfDsec <- parasAfDcon

conf <-mean(FactPredictAfDsec[, 3])
univ <- mean(FactPredictAfDsec[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridAfDsec <-
  seq(
    from = min(FactPredictAfDsec[, 1]),
    to = max(FactPredictAfDsec[, 1]),
    len = 5000
  )

pred.probAfDsec <- list()
pred.probAfDsec.l <- list()
pred.probAfDsec.u <- list()

for (i in 1:length(plot.gridAfDsec)) {
  sec <- plot.gridAfDsec[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasAfDsec[22:29,5];
  coefs.l <- parasAfDsec[22:29,9];
  coefs.u <- parasAfDsec[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasAfDsec[30:37,5];
  coefs.l <- parasAfDsec[30:37,9];
  coefs.u <- parasAfDsec[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasAfDsec[38:45,5];
  coefs.l <- parasAfDsec[38:45,9];
  coefs.u <- parasAfDsec[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasAfDsec[46:53,5];
  coefs.l <- parasAfDsec[46:53,9];
  coefs.u <- parasAfDsec[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasAfDsec[54:61,5];
  coefs.l <- parasAfDsec[54:61,9];
  coefs.u <- parasAfDsec[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasAfDsec[62:69,5];
  coefs.l <- parasAfDsec[62:69,9];
  coefs.u <- parasAfDsec[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasAfDsec[8:21,5];
  coefs.l <- parasAfDsec[8:21,9];
  coefs.u <- parasAfDsec[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probAfDsec[i] <- pnorm(probits);
  pred.probAfDsec.l[i] <- pnorm(probits.l);
  pred.probAfDsec.u[i] <- pnorm(probits.u);
  
  
}

plot.dfAfDsec <-
  as.data.frame(
    cbind(plot.gridAfDsec,pred.probAfDsec,pred.probAfDsec.l,pred.probAfDsec.u),
    names = c("Security Score", "Predicted Probability")
  )

AfDsec <-
  ggplot(plot.dfAfDsec, aes(x = as.numeric(plot.dfAfDsec[, 1]), y = as.numeric(plot.dfAfDsec[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfAfDsec[, 3]), ymax=as.numeric(plot.dfAfDsec[, 4])), alpha = 0.15) +
  labs(subtitle = "AfD",
       x = "Security",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

AfDsec

### UNIVERSALISM ###

### AfD ### (For other parties just replace AfD with the name of the party.)

### Initialization ###
fitModelAfDuniv <- fitAfD

FactPredictAfDuniv <- FactPredictAfDcon

parasAfDuniv <- parasAfDcon

conf <-mean(FactPredictAfDuniv[, 3])
sec <- mean(FactPredictAfDuniv[, 1])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridAfDuniv <-
  seq(
    from = min(FactPredictAfDuniv[, 1]),
    to = max(FactPredictAfDuniv[, 1]),
    len = 5000
  )

pred.probAfDuniv <- list()
pred.probAfDuniv.l <- list()
pred.probAfDuniv.u <- list()


for (i in 1:length(plot.gridAfDuniv)) {
  univ <- plot.gridAfDuniv[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasAfDuniv[22:29,5];
  coefs.l <- parasAfDuniv[22:29,9];
  coefs.u <- parasAfDuniv[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasAfDuniv[30:37,5];
  coefs.l <- parasAfDuniv[30:37,9];
  coefs.u <- parasAfDuniv[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasAfDuniv[38:45,5];
  coefs.l <- parasAfDuniv[38:45,9];
  coefs.u <- parasAfDuniv[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasAfDuniv[46:53,5];
  coefs.l <- parasAfDuniv[46:53,9];
  coefs.u <- parasAfDuniv[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasAfDuniv[54:61,5];
  coefs.l <- parasAfDuniv[54:61,9];
  coefs.u <- parasAfDuniv[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasAfDuniv[62:69,5];
  coefs.l <- parasAfDuniv[62:69,9];
  coefs.u <- parasAfDuniv[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasAfDuniv[8:21,5];
  coefs.l <- parasAfDuniv[8:21,9];
  coefs.u <- parasAfDuniv[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probAfDuniv[i] <- pnorm(probits);
  pred.probAfDuniv.l[i] <- pnorm(probits.l);
  pred.probAfDuniv.u[i] <- pnorm(probits.u);
  
}

plot.dfAfDuniv <-
  as.data.frame(
    cbind(plot.gridAfDuniv,pred.probAfDuniv,pred.probAfDuniv.l,pred.probAfDuniv.u),
    names = c("Universalism Score", "Predicted Probability")
  )

AfDuniv <-
  ggplot(plot.dfAfDuniv, aes(x = as.numeric(plot.dfAfDuniv[, 1]), y = as.numeric(plot.dfAfDuniv[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfAfDuniv[, 3]), ymax=as.numeric(plot.dfAfDuniv[, 4])), alpha = 0.15) +
  labs(subtitle = "AfD",
       x = "Universalism",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

AfDuniv

# Left

### Initialization ###
fitModelLeftcon <- fitLeft

FactPredictLeftcon <- lavPredict(fitModelLeftcon)

parasLeftcon <- parameterEstimates(fitLeft)

sec <-mean(FactPredictLeftcon[, 1])
univ <- mean(FactPredictLeftcon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridLeftcon <-
  seq(
    from = min(FactPredictLeftcon[, 3]),
    to = max(FactPredictLeftcon[, 3]),
    len = 5000
  )

pred.probLeftcon <- list()
pred.probLeftcon.l <- list()
pred.probLeftcon.u <- list()


for (i in 1:length(plot.gridLeftcon)) {
  conf <- plot.gridLeftcon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasLeftcon[22:29,5];
  coefs.l <- parasLeftcon[22:29,9];
  coefs.u <- parasLeftcon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasLeftcon[30:37,5];
  coefs.l <- parasLeftcon[30:37,9];
  coefs.u <- parasLeftcon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasLeftcon[38:45,5];
  coefs.l <- parasLeftcon[38:45,9];
  coefs.u <- parasLeftcon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasLeftcon[46:53,5];
  coefs.l <- parasLeftcon[46:53,9];
  coefs.u <- parasLeftcon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasLeftcon[54:61,5];
  coefs.l <- parasLeftcon[54:61,9];
  coefs.u <- parasLeftcon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasLeftcon[62:69,5];
  coefs.l <- parasLeftcon[62:69,9];
  coefs.u <- parasLeftcon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasLeftcon[8:21,5];
  coefs.l <- parasLeftcon[8:21,9];
  coefs.u <- parasLeftcon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probLeftcon[i] <- pnorm(probits);
  pred.probLeftcon.l[i] <- pnorm(probits.l);
  pred.probLeftcon.u[i] <- pnorm(probits.u);
  
  
}

plot.dfLeftcon <-
  as.data.frame(
    cbind(plot.gridLeftcon,pred.probLeftcon,pred.probLeftcon.l,pred.probLeftcon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

Leftconf <-
  ggplot(plot.dfLeftcon, aes(x = as.numeric(plot.dfLeftcon[, 1]), y = as.numeric(plot.dfLeftcon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfLeftcon[, 3]), ymax=as.numeric(plot.dfLeftcon[, 4])), alpha = 0.15) +
  labs(subtitle = "Left",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

Leftconf

# Universalism

### Initialization ###
fitModelLeftcon <- fitLeft

FactPredictLeftcon <- lavPredict(fitModelLeftcon)

parasLeftcon <- parameterEstimates(fitLeft)

sec <-mean(FactPredictLeftcon[, 1])
univ <- mean(FactPredictLeftcon[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridLeftcon <-
  seq(
    from = min(FactPredictLeftcon[, 3]),
    to = max(FactPredictLeftcon[, 3]),
    len = 5000
  )

pred.probLeftcon <- list()
pred.probLeftcon.l <- list()
pred.probLeftcon.u <- list()

for (i in 1:length(plot.gridLeftcon)) {
  conf <- plot.gridLeftcon[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasLeftcon[22:29,5];
  coefs.l <- parasLeftcon[22:29,9];
  coefs.u <- parasLeftcon[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasLeftcon[30:37,5];
  coefs.l <- parasLeftcon[30:37,9];
  coefs.u <- parasLeftcon[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasLeftcon[38:45,5];
  coefs.l <- parasLeftcon[38:45,9];
  coefs.u <- parasLeftcon[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasLeftcon[46:53,5];
  coefs.l <- parasLeftcon[46:53,9];
  coefs.u <- parasLeftcon[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasLeftcon[54:61,5];
  coefs.l <- parasLeftcon[54:61,9];
  coefs.u <- parasLeftcon[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasLeftcon[62:69,5];
  coefs.l <- parasLeftcon[62:69,9];
  coefs.u <- parasLeftcon[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasLeftcon[8:21,5];
  coefs.l <- parasLeftcon[8:21,9];
  coefs.u <- parasLeftcon[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probLeftcon[i] <- pnorm(probits);
  pred.probLeftcon.l[i] <- pnorm(probits.l);
  pred.probLeftcon.u[i] <- pnorm(probits.u);
  
  
}

plot.dfLeftcon <-
  as.data.frame(
    cbind(plot.gridLeftcon,pred.probLeftcon,pred.probLeftcon.l,pred.probLeftcon.u),
    names = c("Conformity Score", "Predicted Probability")
  )

Leftconf <-
  ggplot(plot.dfLeftcon, aes(x = as.numeric(plot.dfLeftcon[, 1]), y = as.numeric(plot.dfLeftcon[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfLeftcon[, 3]), ymax=as.numeric(plot.dfLeftcon[, 4])), alpha = 0.15) +
  labs(subtitle = "Left",
       x = "Conformity",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

Leftconf

# Security

### Initialization ###
fitModelLeftsec <- fitLeft

FactPredictLeftsec <- FactPredictLeftcon

parasLeftsec <- parasLeftcon

conf <-mean(FactPredictLeftsec[, 3])
univ <- mean(FactPredictLeftsec[, 2])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridLeftsec <-
  seq(
    from = min(FactPredictLeftsec[, 1]),
    to = max(FactPredictLeftsec[, 1]),
    len = 5000
  )

pred.probLeftsec <- list()
pred.probLeftsec.l <- list()
pred.probLeftsec.u <- list()


for (i in 1:length(plot.gridLeftsec)) {
  sec <- plot.gridLeftsec[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasLeftsec[22:29,5];
  coefs.l <- parasLeftsec[22:29,9];
  coefs.u <- parasLeftsec[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasLeftsec[30:37,5];
  coefs.l <- parasLeftsec[30:37,9];
  coefs.u <- parasLeftsec[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasLeftsec[38:45,5];
  coefs.l <- parasLeftsec[38:45,9];
  coefs.u <- parasLeftsec[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasLeftsec[46:53,5];
  coefs.l <- parasLeftsec[46:53,9];
  coefs.u <- parasLeftsec[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasLeftsec[54:61,5];
  coefs.l <- parasLeftsec[54:61,9];
  coefs.u <- parasLeftsec[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasLeftsec[62:69,5];
  coefs.l <- parasLeftsec[62:69,9];
  coefs.u <- parasLeftsec[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasLeftsec[8:21,5];
  coefs.l <- parasLeftsec[8:21,9];
  coefs.u <- parasLeftsec[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probLeftsec[i] <- pnorm(probits);
  pred.probLeftsec.l[i] <- pnorm(probits.l);
  pred.probLeftsec.u[i] <- pnorm(probits.u);
  
  
}

plot.dfLeftsec <-
  as.data.frame(
    cbind(plot.gridLeftsec,pred.probLeftsec,pred.probLeftsec.l,pred.probLeftsec.u),
    names = c("Security Score", "Predicted Probability")
  )

Leftsec <-
  ggplot(plot.dfLeftsec, aes(x = as.numeric(plot.dfLeftsec[, 1]), y = as.numeric(plot.dfLeftsec[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfLeftsec[, 3]), ymax=as.numeric(plot.dfLeftsec[, 4])), alpha = 0.15) +
  labs(subtitle = "Left",
       x = "Security",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

Leftsec

# Universalism 

### Initialization ###
fitModelLeftuniv <- fitLeft

FactPredictLeftuniv <- FactPredictLeftcon

parasLeftuniv <- parasLeftcon

conf <-mean(FactPredictLeftuniv[, 3])
sec <- mean(FactPredictLeftuniv[, 1])
Gender <- 0
Age <- mean(sem.data$Age, na.rm = TRUE)
Income <- mean(sem.data$Income, na.rm = TRUE)
Educ <- mean(sem.data$Educ, na.rm = TRUE)
Relig <- mean(sem.data$Relig, na.rm = TRUE)


### For Loop to Generate Predicted Probabilities ###

plot.gridLeftuniv <-
  seq(
    from = min(FactPredictLeftuniv[, 1]),
    to = max(FactPredictLeftuniv[, 1]),
    len = 5000
  )

pred.probLeftuniv <- list()
pred.probLeftuniv.l <- list()
pred.probLeftuniv.u <- list()


for (i in 1:length(plot.gridLeftuniv)) {
  univ <- plot.gridLeftuniv[i]
  
  vars1 <- cbind(sec,univ,conf,Age,Income,Gender,Educ,Relig)
  
  ####### Immigration 
  
  coefs <- parasLeftuniv[22:29,5];
  coefs.l <- parasLeftuniv[22:29,9];
  coefs.u <- parasLeftuniv[22:29,10];
  
  Immigrat1 <- dot(vars1,coefs);
  Immigrat1.l <- dot(vars1,coefs.l);
  Immigrat1.u <- dot(vars1,coefs.u);
  
  ####### Migrant Integration
  
  coefs <- parasLeftuniv[30:37,5];
  coefs.l <- parasLeftuniv[30:37,9];
  coefs.u <- parasLeftuniv[30:37,10];
  
  MigInteg1 <- dot(vars1,coefs);
  MigInteg1.l <- dot(vars1,coefs.l);
  MigInteg1.u <- dot(vars1,coefs.u);
  
  
  ####### Security Privatization
  
  coefs <- parasLeftuniv[38:45,5];
  coefs.l <- parasLeftuniv[38:45,9];
  coefs.u <- parasLeftuniv[38:45,10];
  
  SecPriv1 <- dot(vars1,coefs);
  SecPriv1.l <- dot(vars1,coefs.l);
  SecPriv1.u <- dot(vars1,coefs.u);
  
  
  ####### EU Integration
  
  coefs <- parasLeftuniv[46:53,5];
  coefs.l <- parasLeftuniv[46:53,9];
  coefs.u <- parasLeftuniv[46:53,10];
  
  EuInteg1 <- dot(vars1,coefs);
  EuInteg.l <- dot(vars1,coefs.l);
  EuInteg.u <- dot(vars1,coefs.u);
  
  
  ####### Socio-Economic
  
  coefs <- parasLeftuniv[54:61,5];
  coefs.l <- parasLeftuniv[54:61,9];
  coefs.u <- parasLeftuniv[54:61,10];
  
  SocEcon1 <- dot(vars1,coefs);
  SocEcon1.l <- dot(vars1,coefs.l);
  SocEcon1.u <- dot(vars1,coefs.u);
  
  
  ####### Climate
  
  coefs <- parasLeftuniv[62:69,5];
  coefs.l <- parasLeftuniv[62:69,9];
  coefs.u <- parasLeftuniv[62:69,10];
  
  Climate1 <- dot(vars1,coefs);
  Climate1.l <- dot(vars1,coefs.l);
  Climate1.u <- dot(vars1,coefs.u);
  
  ###### Linear Prediction
  
  varsp <- cbind(sec,univ,conf,Immigrat1,MigInteg1,SecPriv1,EuInteg1,SocEcon1,Climate1,Age,Income,Gender,Educ,Relig);
  
  coefs <- parasLeftuniv[8:21,5];
  coefs.l <- parasLeftuniv[8:21,9];
  coefs.u <- parasLeftuniv[8:21,10];
  
  probits <- dot(varsp,coefs);
  probits.l <- dot(varsp,coefs.l);
  probits.u <- dot(varsp,coefs.u);
  
  ##### Probability Prediction
  
  pred.probLeftuniv[i] <- pnorm(probits);
  pred.probLeftuniv.l[i] <- pnorm(probits.l);
  pred.probLeftuniv.u[i] <- pnorm(probits.u);
  
  
}

plot.dfLeftuniv <-
  as.data.frame(
    cbind(plot.gridLeftuniv,pred.probLeftuniv,pred.probLeftuniv.l,pred.probLeftuniv.u),
    names = c("Universalism Score", "Predicted Probability")
  )

Leftuniv <-
  ggplot(plot.dfLeftuniv, aes(x = as.numeric(plot.dfLeftuniv[, 1]), y = as.numeric(plot.dfLeftuniv[, 2]))) +
  geom_line() + ylim(0, 1) +
  geom_ribbon(aes(ymin=as.numeric(plot.dfLeftuniv[, 3]), ymax=as.numeric(plot.dfLeftuniv[, 4])), alpha = 0.15) +
  labs(subtitle = "Left",
       x = "Universalism",
       y = "Predicted Probability") + theme_bw() +
  theme(plot.subtitle = element_text(hjust = 0.5))

Leftuniv

# Save Figures 4 and 5 ----------------------------------------------------

grid.arrange(AfDconf, Leftconf, CDUconf, SPDconf, Greensconf, FDPconf, nrow =2)

g <- arrangeGrob(AfDconf, Leftconf, CDUconf, SPDconf, Greensconf, FDPconf, nrow =2) 
ggsave(file="AllPartiesAllValues.pdf", g)


grid.arrange(AfDconf, AfDsec, AfDuniv, Leftconf, Leftsec, Leftuniv, nrow =2)

g <- arrangeGrob(AfDconf, AfDsec, AfDuniv, Leftconf, Leftsec, Leftuniv, nrow =2)
ggsave(file="AfDLeftAll.pdf", g)